package org.geektime.jdbc;

import org.geektime.entity.SRisklistDo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class JdbcTemplateDataManageService {
  @Autowired JdbcTemplate jdbcTemplate;
  private List<SRisklistDo> sRisklistDos;

  @PostConstruct
  public void doOnce() {
    String sql = "select * from s_risklist limit 100";
    sRisklistDos =
        jdbcTemplate.query(
            sql,
                (rs, i) -> {
                  SRisklistDo sRisklistDo = new SRisklistDo();
                  sRisklistDo.setBxqx(rs.getLong("bxqx"));
                  sRisklistDo.setClasscode(rs.getString("classcode"));
                  sRisklistDo.setDataowner(rs.getLong("dataowner"));
                  sRisklistDo.setIdsflag(rs.getString("idsflag"));
                  return sRisklistDo;
                });
    System.out.println("JdbcTemplate doing");
    System.out.println(sRisklistDos);
  }

  public SRisklistDo sRisklistDo(int n) {
    if (n > 0 && n < sRisklistDos.size()) {
      return sRisklistDos.get(n);
    }
    return null;
  }
}
